import { isNumber } from '@/utils'

/** hex转rgb */
function hexToRgb(str) {
  const hxs = str.replace('#', '').match(/../g)
  for (let i = 0; i < 3; i++) hxs[i] = parseInt(hxs[i], 16)
  return hxs
}

function rgbToHex(a, b, c) {
  const hexs = [a.toString(16), b.toString(16), c.toString(16)]
  for (let i = 0; i < 3; i++) {
    if (hexs[i].length == 1) hexs[i] = `0${hexs[i]}`
  }
  return `#${hexs.join('')}`
}

/** 加深颜色值 */
export function darken(color, level) {
  const rgbc = hexToRgb(color)
  for (let i = 0; i < 3; i++) rgbc[i] = Math.floor(rgbc[i] * (1 - level))
  return rgbToHex(rgbc[0], rgbc[1], rgbc[2])
}

/** 变浅颜色值 */
export function lighten(color, level) {
  const rgbc = hexToRgb(color)
  for (let i = 0; i < 3; i++)
    rgbc[i] = Math.floor((255 - rgbc[i]) * level + rgbc[i])
  return rgbToHex(rgbc[0], rgbc[1], rgbc[2])
}

/** 改变基础色透明度 */
export function opciten(color, opcity: number) {
  const rgbc = hexToRgb(color)
  return `rgba(${rgbc[0]}, ${rgbc[1]}, ${rgbc[2]}, ${opcity})`
}

/**
 * 获取随机渐变颜色
 * @param {object} options gradient options
 * @param {boolean} options.randomizeHue 是否使用随机像色
 * @param {boolean} options.brightness 是否使用随机亮度
 * @param {string | number} options.deg 渐变角度
 * @param {number} options.level 渐变级别，2-5
 * @param {string[]} options.colors 基础颜色, 长度必须在2-5之间，设置根据此生成渐变
 * @returns {string}
 */
export function randomGradient(options?: {
  brightness?: boolean
  randomizeHue?: boolean
  deg?: number | string
  level?: number
  colors?: string[]
}) {
  const randomColorFunc = options?.brightness
    ? randomColorBrightness
    : randomColor

  if ((isNumber(options?.level) && options?.level > 5) || options?.level < 2) {
    console.error('level must be between 2 and 5')
  }

  if (options?.colors?.length > 5 || options?.colors?.length < 2) {
    console.error('colors.length must be between 2 and 5')
  }

  // 生成渐变角度
  const direction =
    options?.deg !== undefined ? options.deg : Math.floor(Math.random() * 360)

  // 生成渐变颜色
  // 生成渐变颜色
  let colors: string[]
  if (options?.randomizeHue) {
    colors = Array.from({ length: 2 }, () => randomColorFunc())
  } else {
    if (options?.colors !== undefined) {
      if (options.colors.length < 2 || options.colors.length > 5) {
        console.error('colors.length must be between 2 and 5')
        return ''
      }
      colors = options.colors
    } else {
      if (options?.level !== undefined) {
        if (options.level < 2 || options.level > 5) {
          console.error('level must be between 2 and 5')
          return ''
        }
      }
      colors = Array.from({ length: options?.level || 2 }, () =>
        randomColorFunc()
      )
    }
  }

  // 构建渐变字符串
  const gradientColors = colors.map(color => `${color}`).join(', ')

  // 返回完整的渐变字符串
  return `linear-gradient(${direction}deg, ${gradientColors})`
}

export function randomColor() {
  return `#${Math.floor(Math.random() * 0xffffff)
    .toString(16)
    .padEnd(6, '0')}`
}

export function randomColorBrightness() {
  const minBrightness = 30 // 设置最小亮度阈值
  const randomChannel = () =>
    Math.floor(Math.random() * (256 - minBrightness) + minBrightness)
  return `#${randomChannel().toString(16).padStart(2, '0')}${randomChannel().toString(16).padStart(2, '0')}${randomChannel().toString(16).padStart(2, '0')}`
}
